﻿using System;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;

public partial class ConfigForm : Form
{
    public ConfigForm()
    {
        InitializeComponent();
        this.Icon = new System.Drawing.Icon(this.GetType().Assembly.GetManifestResourceStream("MatrixScreen.MatrixShow.ico"));
    }

    private void ConfigForm_Load(object sender, System.EventArgs e)
    {
        RegistryKey settings = Registry.CurrentUser.OpenSubKey("Software\\MatrixShow");

        if (settings != null)
        {
            chkErrors.Checked = Convert.ToBoolean(settings.GetValue("showerr", false));
            chkUseTint.Checked = Convert.ToBoolean(settings.GetValue("usetint", false));
            chkSubFolders.Checked = Convert.ToBoolean(settings.GetValue("includesub", true));
            chkShowPics.Checked = Convert.ToBoolean(settings.GetValue("showpics", true));
            intervalUpDown.Value = Convert.ToInt32(settings.GetValue("interval", 15));
            maxfpsUpDown.Value = Convert.ToInt32(settings.GetValue("maxFPS", 60));
            chkMsgs.Checked = Convert.ToBoolean(settings.GetValue("showMsgs", true));

            string[] folders = (string[])settings.GetValue("folders", new string[] { Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) });

            foreach (string folder in folders)
            {
                lstDir.Items.Add(folder);
            }
        }
        else
            SetDefaults(false);

    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        //TODO: Rewrite for .NET 2.0
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        DialogResult result = fbd.ShowDialog();

        //why dont these work?
        fbd.ShowNewFolderButton = false;
        fbd.RootFolder = Environment.SpecialFolder.MyPictures;

        if (result == DialogResult.OK)
        {
            if (!lstDir.Items.Contains(fbd.SelectedPath))
                lstDir.Items.Add(fbd.SelectedPath);
            else
                MessageBox.Show("Folder already selected!");
        }
    }

    private void btnRemove_Click(object sender, EventArgs e)
    {
        for (int i = lstDir.SelectedItems.Count - 1; i > -1; i--)
        {
            lstDir.Items.Remove(lstDir.SelectedItems[i]);
        }
    }

    private void btnOk_Click(object sender, EventArgs e)
    {
        RegistryKey settings = Registry.CurrentUser.OpenSubKey("Software\\MatrixShow", true);

        if (settings == null)
            settings = Registry.CurrentUser.CreateSubKey("Software\\MatrixShow");

        settings.SetValue("showerr", chkErrors.Checked);
        settings.SetValue("usetint", chkUseTint.Checked);
        settings.SetValue("includesub", chkSubFolders.Checked);
        settings.SetValue("showpics", chkShowPics.Checked);
        settings.SetValue("interval", intervalUpDown.Value);
        settings.SetValue("maxFPS", maxfpsUpDown.Value);
        settings.SetValue("showMsgs", chkMsgs.Checked);

        string[] folders = new string[lstDir.Items.Count];

        for (int i = 0; i < lstDir.Items.Count; i++)
        {
            folders[i] = (string)lstDir.Items[i];
        }

        settings.SetValue("folders", folders, RegistryValueKind.MultiString);

        this.Close();
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void btnDefaults_Click(object sender, EventArgs e)
    {
        SetDefaults(true);
    }

    private void SetDefaults(bool prompt)
    {
        if (prompt)
        {
            if (MessageBox.Show("Reset to default settings?", "Reset?", MessageBoxButtons.YesNo) == DialogResult.No)
                return;
        }

        chkErrors.Checked = false;
        chkUseTint.Checked = false;
        chkSubFolders.Checked = true;
        chkShowPics.Checked = true;
        chkMsgs.Checked = true;
        intervalUpDown.Value = 15;
        maxfpsUpDown.Value = 60;

        lstDir.Items.Clear();

        lstDir.Items.Add(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));
    }

    private void btnMsgs_Click(object sender, EventArgs e)
    {
        ConfigMessages frm = new ConfigMessages();
        frm.ShowDialog();
        frm.Dispose();
    }
}

